import os
import clr
import time
import subprocess

clr.AddReference("C:/Windows/System32/OpenHardwareMonitorLib.dll")  #填写 OpenHardwareMonitorLib.dll 的路径
from OpenHardwareMonitor.Hardware import Computer 

computer_tmp = Computer()       #实例这这个类
computer_tmp.CPUEnabled = True  #获取CPU温度时用
computer_tmp.GPUEnabled = True  #获取GPU温度时用
computer_tmp.RAMEnabled = True  #获取RAM温度时用
computer_tmp.Open()

GPU_SW = 0
#computer_tmp.HDDEnabled = True #获取HDD温度时用 
# 0 CPU  1 Ram 2 GPU 3 HDD
QtPath = "Detection.exe"
if str(os.popen('nvidia-smi').readlines()).find("CUDA Version") != -1:
    computer_tmp.Hardware[2].Update()
    GPU_SW = 1
userPsth = str(os.popen('echo %USERPROFILE%').readlines())
userPsth = userPsth[2:-4].replace("\\\\","/")   #获取用户路径
#print (userPsth)


def getCpuInfo():
    computer_tmp.Hardware[0].Update()
    computer_tmp.Hardware[1].Update()
    
    cpuTemperature = 0   #cpu温度
    cpuCcont = 0         #cpu时钟计数
    cpuClock = 0         #cpu时钟频率
    cpuLoad  = 0         #cpu占用
    cpuPower = 0         #cpu功耗
    Tcout    = 0         #cpu温度计数
    
    ramLoad  = 0         #ram占用率
    ramUse   = 0         #ram已用
    ramFree  = 0         #ram可用
    
    gpuTemperature = 0
    gpuCoreL = 0
    gpuMem   = 0
    
    #log = open(userPsth+"/DetectionCache.txt","w+")
    log = open("./DetectionCache.txt","w+")
    if GPU_SW == 1:
        computer_tmp.Hardware[2].Update()
        for a in range(0 , len(computer_tmp.Hardware[2].Sensors)):
            #print(str(computer_tmp.Hardware[2].Sensors[a].Identifier),computer_tmp.Hardware[2].Sensors[a].Value)
            if ( str(computer_tmp.Hardware[2].Sensors[a].Identifier).find("temperature") != -1): 
                gpuTemperature =  float(computer_tmp.Hardware[2].Sensors[a].Value)
            
            if ( str(computer_tmp.Hardware[2].Sensors[a].Identifier).find("/load/0") != -1): 
                gpuCoreL =  float(computer_tmp.Hardware[2].Sensors[a].Value)
                
            if ( str(computer_tmp.Hardware[2].Sensors[a].Identifier).find("/load/4") != -1): 
                gpuMem =  float(computer_tmp.Hardware[2].Sensors[a].Value)

        print("gpuCL:" , round(gpuCoreL, 2),        file = log)
        print("gpuT:"  , round(gpuTemperature , 2), file = log)
        print("gpuM:"  , round(gpuMem, 2),          file = log)
        
    for a in range(0 , len(computer_tmp.Hardware[1].Sensors)):
        #print(str(computer_tmp.Hardware[1].Sensors[a].Identifier),computer_tmp.Hardware[1].Sensors[a].Value)
        if ( str(computer_tmp.Hardware[1].Sensors[a].Identifier).find("load") != -1): 
            ramLoad =  float(computer_tmp.Hardware[1].Sensors[a].Value)
            
        if ( str(computer_tmp.Hardware[1].Sensors[a].Identifier).find("data/0") != -1): 
            ramUse =  float(computer_tmp.Hardware[1].Sensors[a].Value)
            
        if ( str(computer_tmp.Hardware[1].Sensors[a].Identifier).find("data/1") != -1): 
            ramFree =  float(computer_tmp.Hardware[1].Sensors[a].Value)
            
    print("ramL:" , round(ramLoad, 2), file = log)
    print("ramU:" , round(ramUse , 2), file = log)
    print("ramF:" , round(ramFree, 2), file = log)
    
    for a in range(0, len(computer_tmp.Hardware[0].Sensors)):
        #print(computer_tmp.Hardware[0].Sensors[a].Identifier,computer_tmp.Hardware[0].Sensors[a].Value)
        if ( str(computer_tmp.Hardware[0].Sensors[a].Identifier).find("temperature") != -1):  
            Tcout = Tcout + 1
            cpuTemperature = float(computer_tmp.Hardware[0].Sensors[a].Value) + cpuTemperature
            
        if ( str(computer_tmp.Hardware[0].Sensors[a].Identifier).find("clock") != -1):
            if int(computer_tmp.Hardware[0].Sensors[a].Value) > 300:
                cpuCcont = cpuCcont + 1
                cpuClock = float(computer_tmp.Hardware[0].Sensors[a].Value) + cpuClock
        
        if ( str(computer_tmp.Hardware[0].Sensors[a].Identifier).find("load/0") != -1): 
            cpuLoad = float(computer_tmp.Hardware[0].Sensors[a].Value)
         
        if ( str(computer_tmp.Hardware[0].Sensors[a].Identifier).find("power/0") != -1):   
            cpuPower = float(computer_tmp.Hardware[0].Sensors[a].Value)
    
    if Tcout == 0:
        Tcout = 1
    if cpuCcont == 0:
        cpuCcont = 1
    cpuTemperature = cpuTemperature / Tcout 
    cpuClock = cpuClock / cpuCcont
    print("cpuT:" , round(cpuTemperature,2),  file = log)
    print("cpuF:" , round(cpuClock,2),        file = log)
    print("cpuL:" , round(cpuLoad,2) ,        file = log)
    print("cpuP:" , round(cpuPower,2),        file = log)
            
def __main():
    #if str(os.popen('tasklist /FI "imagename eq Detection.exe"').readlines()).find("没有运行的任务匹配指定标准") != -1:
    #    subprocess.Popen(QtPath)            
    while True:
    #    if str(os.popen('tasklist /FI "imagename eq Detection.exe"').readlines()).find("没有运行的任务匹配指定标准") != -1:
    #        os._exit(0)
        time.sleep(1)
        getCpuInfo()
__main()
